package cn.tedu.travelsystem.travelDownload.service.impl;

import cn.tedu.travelsystem.travelDownload.service.FileStorageService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.*;

@Service
public class FileStorageServiceImpl implements FileStorageService {

    @Value("${file.upload-dir}")
    private String uploadDir;

    @Override
    public String storeAvatar(MultipartFile file) throws IOException {
        String filename = "avatar_" + System.currentTimeMillis() +
                file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        Path targetPath = Paths.get(uploadDir).resolve(filename);
        Files.createDirectories(targetPath.getParent());
        Files.copy(file.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
        return filename;
    }
}